export  class TreeUtils {
  //根据最后一个找到整个家族(家谱树)
  public familyTree(arr,pid){
    var temp = [];
    var forFn = function(arr,pid){
      for(var i = 0;i<arr.length;i++){
        var item = arr[i];
        if(item.id == pid){
          temp.push(item);
          forFn(arr,item.pId);
        }
      }
    };
    forFn(arr,pid);
    return temp;
  }
  //从顶级往下找到所有子子孙孙(子孙树)
  public sonsTree(arr,id){
    var temp = [],lev=1;
    var forFn = function(arr,id,lev){
      for(var i=0;i<arr.length;i++){
        var item = arr[i];
        if(item.pId==id){
          item.lev = lev;
          temp.push(item);
          forFn(arr,item.id,lev+1);
        }
      }
    };
    forFn(arr,id,lev);
    return temp;
  }
  public getTreeHtml(data){
    var tree = this.sonsTree(data,1);
    var temp = []
    for(var i =0;i<tree.length;i++){
      var item = tree[i],u="";
      if(i>0){
        u="</ul>";
      }
      if(item['lev']==1){
        temp.push(u+'<ul><a class="one">'+item.name+'</a><ul>');
      }else{
        temp.push('<li><a>'+item.name+'</a>');
      }
      if(i+1==tree.length){
        temp.push("</ul>");
      }
    }
    return temp;

  }
}
